#include "board_tim.h"
#include "board_config.h"

TIM_HandleTypeDef trigger_tim_handler;

void trigger_timer_init(void)
{
	uint32_t uwPrescalerValue = (uint32_t) ((SystemCoreClock /2) / 1000000) - 1;	/* micro second */

	/* Set TIMx instance */
	trigger_tim_handler.Instance = TRIGGER_TIMER;

	trigger_tim_handler.Init.Period = 1000000 / TRIGGER_FREQ - 1;
	trigger_tim_handler.Init.Prescaler = uwPrescalerValue;
	trigger_tim_handler.Init.ClockDivision = 0;
	trigger_tim_handler.Init.CounterMode = TIM_COUNTERMODE_UP;

	HAL_TIM_Base_Init(&trigger_tim_handler);

	HAL_TIM_Base_Start_IT(&trigger_tim_handler);
}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
{
	if (htim == &trigger_tim_handler) {
		/*##-1- Enable peripherals and GPIO Clocks #################################*/
		/* TIMx Peripheral clock enable */
		TRIGGER_TIMER_CLK_ENABLE();

		/*##-2- Configure the NVIC for TIMx ########################################*/
		/* Set Interrupt Group Priority */
		HAL_NVIC_SetPriority(TRIGGER_TIMER_IRQn, 1, 0);

		/* Enable the TIMx global Interrupt */
		HAL_NVIC_EnableIRQ(TRIGGER_TIMER_IRQn);
	}
}
